import { _decorator, Component, Node, Label, Button, Sprite, director, Material } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import YangLeGeYang_GameFunction from "../Manager/YangLeGeYang_GameFunction";
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import YangLeGeYang_GameManager from "../Manager/YangLeGeYang_GameManager";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

enum Colors {
        黄 = `#FFF2D7`,
        绿 = `#CAEAD8`,
        灰 = `#F4F4F4`,
        蓝 = `#E0EFFF`,
        粉 = `#FFEAEF`,
        紫 = `#E6E3F6`,
        羊蓝 = `#A5E2E8`,
        羊绿 = `#CDE6C5`,
}

@ccclass('YangLeGeYangGamePanel')
export default class YangLeGeYang_GamePanel extends Component {
        public static Instance: YangLeGeYang_GamePanel;
        @property(Node)
        public Button_back: Node | null = null;
        @property(Node)
        Button_refresh: Node | null = null;
        @property(Node)
        public Button_setting: Node | null = null;
        @property(Label)
        public Label_level: Label | null = null;
        //    // bottom
        @property(Button)
        public Button_1: Button | null = null;
        @property(Button)
        public Button_2: Button | null = null;
        @property(Button)
        public Button_3: Button | null = null;
        @property(Sprite)
        public Label_Button_1: Sprite | null = null;
        @property(Sprite)
        public Label_Button_2: Sprite | null = null;
        @property(Sprite)
        public Label_Button_3: Sprite | null = null;
        @property(Node)
        public level_progress_1: Node | null = null;
        @property(Node)
        public level_progress_2: Node | null = null;
        @property([Node])
        public bg_list: Node[] = [];
        @property(Node)
        public bg: Node | null = null;

        @property(Material)
        public material_normal: Material | null = null;
        @property(Material)
        public material_gray: Material | null = null;

        onLoad() {
                YangLeGeYang_GamePanel.Instance = this;

                // this.ShowOn();
        }
        ShowOn() {
                this.node.active = true;
                this.AddListener();
                this.Refresh();

                this.Refresh_bg();
        }
        getRandomEnumValue<T>(enumObj: T): T[keyof T] {
                // 获取所有字符串键（过滤掉数字键，因为数字枚举有反向映射）
                const enumKeys = Object.keys(enumObj).filter(key => isNaN(Number(key)));
                const randomIndex = Math.floor(Math.random() * enumKeys.length);
                const randomKey = enumKeys[randomIndex];
                return enumObj[randomKey as keyof T];
        }
        Refresh_bg() {
                // let random_index = randomRangeInt(0, this.bg_list.length);

                // const randomColor = this.getRandomEnumValue(Colors); // 随机 Color 值

                // const randomColor = this.getRandomEnumValue(SSS_Consts.Colors); // 随机 Color 值
                // console.log(`randomColor`, randomColor);

                // // let color = SSS_Consts.Colors[randomColor];
                // this.bg.color = new Color().fromHEX(randomColor);


                // let random_index = randomRangeInt(0, this.bg_list.length);
                // for (let index = 0; index < this.bg_list.length; index++) {
                //     const node = this.bg_list[index];
                //     node.active = index === random_index;
                // }
        }
        ShowOff() {
                this.node.active = false;
                this.RemoveListener();
        }
        Refresh() {
                // this.level_progress_2.active = DanLeGeCi_GameData.CurrentLevel === 1;

                this.Button_1.interactable = YangLeGeYang_GameData.LevelUseItemNumber[0] > 0;
                this.Button_2.interactable = YangLeGeYang_GameData.LevelUseItemNumber[1] > 0;
                this.Button_3.interactable = YangLeGeYang_GameData.LevelUseItemNumber[2] > 0;

                this.Label_Button_1.node.active = YangLeGeYang_GameData.LevelUseItemNumber[0] > 0;
                this.Label_Button_2.node.active = YangLeGeYang_GameData.LevelUseItemNumber[1] > 0;
                this.Label_Button_3.node.active = YangLeGeYang_GameData.LevelUseItemNumber[2] > 0;


                this.Button_1.getComponent(Sprite).material = this.Button_1.interactable ? this.material_normal : this.material_gray;
                this.Button_2.getComponent(Sprite).material = this.Button_2.interactable ? this.material_normal : this.material_gray;
                this.Button_3.getComponent(Sprite).material = this.Button_3.interactable ? this.material_normal : this.material_gray;


                // this.Label_Button_1.node = YangLeGeYang_GameData.LevelUseItemNumber[0];
                // this.Label_Button_2.node = YangLeGeYang_GameData.LevelUseItemNumber[1];
                // this.Label_Button_3.node = YangLeGeYang_GameData.LevelUseItemNumber[2];

                // this.Label_level.string = `第${YangLeGeYang_GameData.CurrentLevelIndex + 1}关`
                this.Label_level.string = `第${YangLeGeYang_GameData.Get_CurrentLevelIndex() + 1}关`


                // let levelList = DanLeGeCi_GameData.Get_LevelList();
                // const item_data = levelList.wordList[DanLeGeCi_GameData.CurrentLevelIndex];
                // this.Label_level.string = `${item_data.cn}`;
        }
        AddListener() {
                this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
                this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

                // this.Button_setting.on(Node.EventType.TOUCH_END, this.On_Button_setting, this);
                this.Button_1.node.on(Node.EventType.TOUCH_END, this.On_Button_1, this);
                this.Button_2.node.on(Node.EventType.TOUCH_END, this.On_Button_2, this);
                this.Button_3.node.on(Node.EventType.TOUCH_END, this.On_Button_3, this);
        }
        RemoveListener() {
                this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
                this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

                // this.Button_setting.off(Node.EventType.TOUCH_END, this.On_Button_setting, this);
                this.Button_1.node.off(Node.EventType.TOUCH_END, this.On_Button_1, this);
                this.Button_2.node.off(Node.EventType.TOUCH_END, this.On_Button_2, this);
                this.Button_3.node.off(Node.EventType.TOUCH_END, this.On_Button_3, this);
        }
        On_Button_back() {
                director.loadScene(SSS_Consts.SceneName.MainScene);
                SSS_AudioManager.PlayButtonAudio();
        }
        On_Button_refresh() {
                YangLeGeYang_GameManager.Instance.Replay();

                SSS_AudioManager.PlayButtonAudio();
        }
        On_Button_setting() {
                // AudioManager.Play_sound("click", false, 1);
                // SettingPanel.Instance.ShowOn();
        }

        On_Button_1() {
                console.log(`On_Button_1-移出`);
                if (YangLeGeYang_GameData.LevelUseItemNumber[0] > 0) {
                        let isUse = YangLeGeYang_GameFunction.RemoveOperatePanel();
                        if (isUse) {
                                YangLeGeYang_GameData.LevelUseItemNumber[0]--;
                                this.Refresh();
                        }
                }
                SSS_AudioManager.PlayButtonAudio();
        }
        On_Button_2() {
                console.log(`On_Button_2-洗牌`);
                if (YangLeGeYang_GameData.LevelUseItemNumber[1] > 0) {
                        let isUse = YangLeGeYang_GameFunction.WashCards();
                        if (isUse) {
                                YangLeGeYang_GameData.LevelUseItemNumber[1]--;
                                this.Refresh();
                        }
                }
                SSS_AudioManager.PlayButtonAudio();
        }
        On_Button_3() {
                console.log(`On_Button_3-撤回`);
                if (YangLeGeYang_GameData.LevelUseItemNumber[2] > 0) {
                        let isUse = YangLeGeYang_GameFunction.Ctrl_Z();
                        if (isUse) {
                                YangLeGeYang_GameData.LevelUseItemNumber[2]--;
                                this.Refresh();
                        }
                }
                SSS_AudioManager.PlayButtonAudio();
        }
}